package com.zhangh.summer.admin.biz.app.impl;

import com.zhangh.summer.admin.biz.app.AppBiz;
import com.zhangh.summer.admin.model.bo.DataBO;
import com.zhangh.summer.admin.model.bo.NoValidatorBO;
import com.zhangh.summer.admin.model.po.*;
import com.zhangh.summer.admin.support.ds.common.MixedDS;
import com.zhangh.summer.admin.support.ds.common.pager.DataPager;
import com.zhangh.summer.admin.support.ds.common.pager.Pager;
import com.zhangh.summer.admin.support.exception.BizException;
import com.zhangh.summer.admin.support.id.IdGen;
import com.zhangh.summer.admin.support.session.SysSession;
import com.zhangh.summer.admin.support.utils.BeanGenerator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @description: TODO
 * @author: zhanghui
 * @create: 2021-10-21 23:22
 **/
@Service
@Slf4j
public class AppBizImpl implements AppBiz {
    @Resource
    private MixedDS mixedDS;
    @Autowired
    private IdGen idGen;

    @Override
    public DataPager<AdAppPO> getAppDataPager(DataBO query, Pager pager) {
        AdAppPOExample example=new AdAppPOExample();
        AdAppPOExample.Criteria c=example.createCriteria();
        if (StringUtils.isNotEmpty(query.getName())){
            c.andAppNameLike("%"+query.getName()+"%");
            example.or().andAppCodeEqualTo(query.getName());
        }
        DataPager<AdAppPO> dataPager=this.mixedDS.txDs().mapper().selectDataPager(example,pager);
        return dataPager;
    }

    @Override
    public AdAppPO getAdAppPO(Long id) {
        AdAppPO adAppPO=this.mixedDS.txDs().mapper().selectByPrimaryKey(AdAppPO.class,id);
        return adAppPO;
    }

    @Override
    @Transactional
    public long saveApp(AdAppPO adAppPO) {
        adAppPO.setId(idGen.getID());
        adAppPO.setCreateBy(SysSession.getUser().getLoginName());
        adAppPO.setCreateTime(new Date());
        adAppPO.setUpdateBy(SysSession.getUser().getLoginName());
        adAppPO.setUpdateTime(new Date());
        BeanGenerator.fillNull(adAppPO);
        int c1=this.mixedDS.txDs().mapper().insert(adAppPO);
        log.info("保存应用 id {}",adAppPO.getId());
        return adAppPO.getId();
    }

    @Override
    @Transactional
    public int updateApp(AdAppPO adAppPO) {
        adAppPO.setUpdateBy(SysSession.getUser().getLoginName());
        adAppPO.setUpdateTime(new Date());
        int c1=this.mixedDS.txDs().mapper().updateByPrimaryKeySelective(adAppPO);
        log.info("更新应用 id {}",adAppPO.getId());
        return c1;
    }

    @Override
    @Transactional
    public int deleteApp(long id) {
        AdMenuPOExample adMenuPOExample=new AdMenuPOExample();
        adMenuPOExample.createCriteria().andAppIDEqualTo(id);
        long c1=this.mixedDS.txDs().mapper().countByExample(adMenuPOExample);
        if (c1>0){
            log.error("应用id {}已经被使用，不可删除",id);
            throw BizException.exception("应用id {0}已经被使用，不可删除",id);
        }
        int c2=this.mixedDS.txDs().mapper().deleteByPrimaryKey(AdAppPO.class,id);
        log.info("删除应用 id {}",id);
        return c2;
    }

    @Override
    public boolean validateAppCode(NoValidatorBO noValidatorBO) {
        AdAppPOExample example=new AdAppPOExample();
        if (StringUtils.isNotBlank(noValidatorBO.getOriginalNo())){
            if (noValidatorBO.getNewNo().equals(noValidatorBO.getOriginalNo())){
                return true;
            }
        }
        example.createCriteria().andAppCodeEqualTo(noValidatorBO.getNewNo());
        long c1=this.mixedDS.txDs().mapper().countByExample(example);
        if (c1==0){
            return true;
        }
        return false;
    }

    @Override
    public List<AdAppPO> getAdAppPOList(Integer frame) {
        AdAppPOExample example=new AdAppPOExample();
        AdAppPOExample.Criteria c=example.createCriteria();
        if (frame!=null && frame > 0){
            c.andFrameEqualTo(frame);
        }
        example.setOrderByClause("createTime asc");
        List<AdAppPO> list=this.mixedDS.txDs().mapper().selectByExample(example);
        return list;
    }
}
